package com.littlebee.storysuper.utils;

import android.app.Activity;
import android.os.Environment;
import android.text.TextUtils;

import com.geepi.coremodule.FileUtil;
import com.littlebee.storysuper.BaseApplication;
import com.littlebee.storysuper.common.sharePrefUtils.SharePrefUtil;
import com.littlebee.storysuper.entry.User;
import com.zss.library.utils.SharedPrefUtils;

import java.io.File;

/**
 * Created by zm on 2017/2/28.
 */

public class Utils {

    public static void commonError(Activity activity, String result, String code, String msg){
        if(TextUtils.isEmpty(code) && TextUtils.isEmpty(msg)){
            CommonToastUtils.showInCenterToast(activity, "连接超时");
        }else{
            CommonToastUtils.showInCenterToast(activity, msg+"("+code+")");
        }
    }
    public static boolean isLogin() {
        return getUserEntry() != null;
    }
    public static User getUserEntry() {
        User user = (User) SharePrefUtil.getInstance(BaseApplication.getInstance()).getObject(SharePrefUtil.kAccountInfo);
        return user;
    }
    public static SharedPrefUtils getSharedPrefCommonFile() {
        return new SharedPrefUtils(BaseApplication.getInstance(), "childapp");
    }

    public static void saveUserEntry(User user) {
        SharePrefUtil.getInstance(BaseApplication.getInstance()).setObject(SharePrefUtil.kAccountInfo, user);
    }

    /**
     * 判断SD卡是否mounted
     *
     * @return isMounted boolean 已挂载SDCard返回true，否则返回false
     */
    public static boolean isMounted() {
        boolean isMounted = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);
        return isMounted;
    }

    /**
     * SD卡管理器 获取Path
     *
     * @return SDCardPath String 已挂载SDCard返回SDCard路径，否则返回"/"(根路径)
     */
    public static String getSDCardPath() {
        String SDCardPath = "";
        if (isMounted()) {
            SDCardPath = Environment.getExternalStorageDirectory().getPath();
            return SDCardPath + File.separator;
        }

        SDCardPath = DevMountUtil.getInstance().getInternalSDCardPath();
        if (SDCardPath != null && !SDCardPath.equals("")) {
            ToastUtil.displayShortToast("internalSDCARD.....");
            return SDCardPath + File.separator;
        }

        SDCardPath = Constant.kRootPath + File.separator;
        return SDCardPath;
    }

    /*path manager*/
    public static String getApplicationRootPath() {
        String appRootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator;
        try {
            LogUtil.d("obtain app path error1");
            if (FileUtil.mkDirs(appRootPath)) {
                return appRootPath;
            } else {
                LogUtil.d("obtain app path error1");
                return "/";
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.d("obtain app path error2");
            return "/";
        }
    }

    /**
     * 返回音频根路径文件夹
     *
     * @return
     */
    public static String getVoiceRootPath() {
        String rootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator + "voice" + File.separator;
        try {
            if (FileUtil.mkDirs(rootPath)) {
                return rootPath;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
